package com.njwb.Swing.Bill;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

public class BillView extends JPanel {
    public JTable billTable;
    public DefaultTableModel billTableModel;
    public JButton addButton, editButton, deleteButton, exportButton, searchButton;
    public JTextField searchField;
    public JComboBox<String> paidBox;

    public BillView() {
        setLayout(new BorderLayout(5, 5));
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 顶部操作按钮 + 搜索框面板
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
        topPanel.setBackground(new Color(240, 248, 255));
        topPanel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));

        searchField = new JTextField(12);
        paidBox = new JComboBox<>(new String[]{"全部", "已付款", "未付款"});
        searchButton = new JButton("搜索");

        addButton = new JButton("新增账单");
        editButton = new JButton("修改账单");
        deleteButton = new JButton("删除账单");
        exportButton = new JButton("导出账单");

        Dimension btnSize = new Dimension(100, 28);
        addButton.setPreferredSize(btnSize);
        editButton.setPreferredSize(btnSize);
        deleteButton.setPreferredSize(btnSize);
        exportButton.setPreferredSize(btnSize);

        topPanel.add(Box.createHorizontalStrut(5));
        topPanel.add(new JLabel("商品名称:"));
        topPanel.add(Box.createHorizontalStrut(5));
        topPanel.add(searchField);
        topPanel.add(Box.createHorizontalStrut(10));
        topPanel.add(new JLabel("是否付款:"));
        topPanel.add(Box.createHorizontalStrut(5));
        topPanel.add(paidBox);
        topPanel.add(Box.createHorizontalStrut(10));
        topPanel.add(searchButton);
        topPanel.add(Box.createHorizontalStrut(10));
        topPanel.add(addButton);
        topPanel.add(Box.createHorizontalStrut(5));
        topPanel.add(editButton);
        topPanel.add(Box.createHorizontalStrut(5));
        topPanel.add(deleteButton);
        topPanel.add(Box.createHorizontalStrut(5));
        topPanel.add(exportButton);
        topPanel.add(Box.createHorizontalGlue());

        add(topPanel, BorderLayout.NORTH);

        // 表格
        String[] columnNames = {"编号", "商品名称", "数量", "单位", "交易金额", "是否付款", "供应商名称", "商品描述", "账单时间"};
        billTableModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) { return false; }
        };
        billTable = new JTable(billTableModel);
        billTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        billTable.setRowHeight(28);
        billTable.setFont(new Font("宋体", Font.PLAIN, 14));
        billTable.getTableHeader().setFont(new Font("宋体", Font.BOLD, 14));
        billTable.getTableHeader().setBackground(new Color(200, 220, 240));
        billTable.setSelectionBackground(new Color(173, 216, 230));

        // 隐藏单位列
        billTable.getColumnModel().getColumn(3).setMinWidth(0);
        billTable.getColumnModel().getColumn(3).setMaxWidth(0);

        JScrollPane scrollPane = new JScrollPane(billTable);
        scrollPane.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        add(scrollPane, BorderLayout.CENTER);
    }
}
